//
//  XinCoffeeHeaderView.swift
//  XinGuoSearch
//
//  Created by 李东 on 2024/4/9.
//

import UIKit

class XinCoffeeHeaderView: UIView {
    
    lazy var contentView = UIView()
    lazy var topLabel = UILabel()
    lazy var mgLabel = UILabel()
    lazy var mgBottomLabel = UILabel()
    lazy var countLabel = UILabel()
    lazy var countBottomLabel = UILabel()
    lazy var summaryLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        contentView.backgroundColor = .init(hex: 0xFFB000)
        contentView.layer.cornerRadius = 10
        contentView.layer.masksToBounds = true
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.bottom.equalToSuperview()
        }
        
        topLabel.textColor = .black
        topLabel.font = UIFont.Semibold(ofSize: 18)
        topLabel.text = "咖啡因摄入"
        contentView.addSubview(topLabel)
        topLabel.snp.makeConstraints { make in
            make.left.top.equalTo(12)
            make.height.equalTo(25)
        }
        
        mgLabel.textColor = .white
        mgLabel.font = UIFont.Semibold(ofSize: 14)//26
        mgLabel.textAlignment = .center
        mgLabel.backgroundColor = .init(hex: 0x55422D)
        mgLabel.layer.cornerRadius = 27
        mgLabel.layer.masksToBounds = true
        contentView.addSubview(mgLabel)
        mgLabel.snp.makeConstraints { make in
            make.right.equalTo(self.snp.centerX).offset(-6)
            make.height.equalTo(54)
            make.width.equalTo(120)
            make.top.equalTo(topLabel.snp.bottom).offset(20)
        }
        
        countLabel.textColor = .white
        countLabel.font = UIFont.Semibold(ofSize: 14)//26
        countLabel.textAlignment = .center
        countLabel.backgroundColor = .init(hex: 0x55422D)
        countLabel.layer.cornerRadius = 27
        countLabel.layer.masksToBounds = true
        contentView.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.left.equalTo(self.snp.centerX).offset(6)
            make.height.width.centerY.equalTo(mgLabel)
        }
        
        mgBottomLabel.text = "当前咖啡因"
        mgBottomLabel.textColor = .black
        mgBottomLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(mgBottomLabel)
        mgBottomLabel.snp.makeConstraints { make in
            make.centerX.equalTo(mgLabel)
            make.top.equalTo(mgLabel.snp.bottom).offset(12)
            make.height.equalTo(20)
        }
        
        countBottomLabel.text = "今日饮用"
        countBottomLabel.textColor = .black
        countBottomLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(countBottomLabel)
        countBottomLabel.snp.makeConstraints { make in
            make.centerX.equalTo(countLabel)
            make.top.equalTo(countLabel.snp.bottom).offset(12)
            make.height.equalTo(20)
        }
        
        summaryLabel.backgroundColor = .white
        summaryLabel.layer.cornerRadius = 4
        summaryLabel.layer.masksToBounds = true
        summaryLabel.textAlignment = .center
        summaryLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(summaryLabel)
        summaryLabel.snp.makeConstraints { make in
            make.left.equalTo(6)
            make.right.equalTo(-6)
            make.bottom.equalTo(-8)
            make.height.equalTo(40)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reloadData(today: String = "")  {
        let list = XinDataHelper.shared.getLocalList()
        var mg = 0
        var count = 0
        list.forEach { mm in
            if mm.ymd == today {
                mg += mm.mg
                count = mm.count
            }
        }
        let attri = NSMutableAttributedString(string: "\(mg) mg")
        attri.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 26), range: NSRange(location: 0, length: "\(mg)".length))
        mgLabel.attributedText = attri
        
        let attri1 = NSMutableAttributedString(string: "\(count) 杯")
        attri1.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 26), range: NSRange(location: 0, length: "\(count)".length))
        
        countLabel.attributedText = attri1
        countLabel.adjustsFontSizeToFitWidth = true
        
        let pre = String(format: "%.2f%%", CGFloat(mg)/CGFloat(300) * 100)
        summaryLabel.text = "今日上限（300mg） 已完成" + pre
        summaryLabel.adjustsFontSizeToFitWidth = true
    }

}
